<template>
	<view class="content">
		<view class="pad-lr-20">
			<view class="mar-top-30 mb10 display-row-reverse fontSize-28">
				<text @click="changestate">{{state?'管理':'退出管理'}}</text>
			</view>
			<view class="mar-top-20 pad-lr-20 pad-tb-20 radius-16 bgfff" v-for="(item,ix) in list" :key="ix">
				<view class="mar-top-20  display align-items"  @click="gogoodsdetails(item.goods_id)" :class="(item.stock==0||item.status==0)&&state?'active':''">
					<image src="/static/img/icon_weixuanzhong.png" class="proudct-item-icon-15" @click.stop="changegoods(item.id,item.stock,item.status)" v-show="!item.check" mode=""></image>
					<image src="/static/img/icon_xuanzhong.png" class="proudct-item-icon-15" @click.stop="changegoods(item.id,item.stock,item.status)" v-show="item.check" mode=""></image>
					<image :src="item.image_text" class="ml20 proudct-item-icon-10	 radius-16" mode=""></image>
					<view class="ml20 right_text">
						<view class=" display align-items fontSize-28">
							<view class="text-clamp-1 fontSize-28">{{item.name}}</view>
						</view>
						<view class="fontSize-26 col-E23737" v-if="item.status==0">商品无效</view>
						<view class="fontSize-26 col-E23737" v-if="item.stock==0">库存不足，无法购买</view>
						<view class="mar-top-20 fontSize-26 col-bc8c1e attrname">
							{{item.sku_name}} 
						</view>
						<view class="display-sb align-items mar-top-22 width-100">
							<view class=" fontSize-28 col-E23737"><text class="fontSize-20">￥</text>{{item.price}}</view>
							<view class="bujinqi"  @click.stop>
							<tm-stepper width="150" :defaultValue="item.num" v-model="item.num" :disabled="item.stock==0||item.status==0" :max="item.stock" @change="changenum(item,$event)" :min="1"></tm-stepper>
							</view>
						</view>
					</view>
				</view>				
			</view>
			<view class="display-ce align-items" v-if="list?.length==0">
				<tm-result :showBtn="false" subTitle=" " title="暂无数据"  color="#bc8c1e"></tm-result>
			</view>
		</view>
		<view class="poFi bgfff width-100 bottom-0 left-0" style="z-index: 99;">
			<view class="pad-tb-20 pad-lr-20 display-sb align-items">
				<view class="display align-items" @click="changeall">
				<image src="/static/img/icon_weixuanzhong.png" class="proudct-item-icon-15" v-show="!is_all" mode=""></image>
				<image src="/static/img/icon_xuanzhong.png" class="proudct-item-icon-15" v-show="is_all" mode=""></image>
				<text class="ml10 fontSize-28">全选</text>
				</view>
				<view class="display align-items">
					<view class="fontSize-28" v-show="state">合计：<text class="fontSize-30 col-E23737">￥{{select_price.toFixed(2)}}</text></view>
					<view class="col-f ml20 radius-30 fontSize-26 jie_btn display-ce align-items" @click="opendeliverytype" v-show="state">结算</view>
					<view class="col-f ml20 radius-30 fontSize-26 jie_btn display-ce align-items" @click="delgoods" v-show="!state">删除</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script lang="ts" setup>
import { ref } from 'vue';
import {cartList,cart_sett,saveCart,delCart} from '@/utils/api'
import { onLoad, onShow } from '@dcloudio/uni-app';
onLoad(()=>{
})
onShow(()=>{
	is_all.value = false
	select_price.value =0
	_cart_list()
})
const state = ref(true)//判断是否删除状态
const changestate =() =>{
	state.value = !state.value
	list.value.forEach(item=>{
			if(state.value){
				if(item.stock==0||item.status==0){
					item.check = false
					select_price.value-=(item.price*item.num)
				}
			}
		
	})
}
//购物车列表
const list = ref()
const _cart_list =() =>{
	cartList().then(res=>{
		if(res.code==1){
			res.data.data.forEach(item=>{
					item.check = false					
			})
			list.value = res.data.data
		}
	})
}
const is_all = ref(false)//是否全选
const select_price = ref(0)//选择商品的金额

const type_two_array = ref()//自提商品列表
const changegoods =(id:number,num:number,is_shelves:number) =>{
	if((num==0||is_shelves==0)&&state.value){
		return
	}
	list.value.forEach(it=>{
			if(it.id == id){
				if(it.check){
					it.check = false
					select_price.value-=(it.price*it.num)
				}else{
					it.check = true
					select_price.value+=(it.price*it.num)
				}
			}
	})
	if(isAllTrue(list.value)){
		is_all.value = true
	}else{
		is_all.value = false
	}
}
const isAllTrue = (arr) =>{
	 for (let row of arr) {
	      if (row.check !== true) {
	        return false; // 一旦发现非 true 值，直接返回 false
	      }
	  }
	  return true; // 如果循环结束未返回 false，说明全为 true
}
//全选
const changeall =() =>{
	is_all.value = !is_all.value
	select_price.value = 0
	list.value.forEach(it=>{
			if(is_all.value){
				if(!state.value){
					it.check = true
					select_price.value+=(it.price*it.num)
				}else{
					if(it.stock>0&&it.status==1){
						it.check = true
						select_price.value+=(it.price*it.num)
					}
				}
			}else{
				it.check = false
				select_price.value+=0
			}
	})
}
const changenum = (a,e) =>{
	console.log(a,e)
	let obj = {
		id:a.goods_id,
		num:e,
		scene:1,
		sku_code:a.sku_code
	}
	saveCart(obj).then(res=>{
		if(res.code == 1){
			_cart_list();
			is_all.value = false
			select_price.value = 0
		}else{
			uni.showToast({
				title:res.msg,
				icon:'none'
			})
			list.value.forEach(item=>{
				item.cart_data.forEach(it=>{
					if(it.id==a.id){
						console.log(56)
						it.num = e-1
					}
				})
			})
		}
	})
}
const delgoods =() =>{
	uni.showModal({
			title:'提示',
			content:'是否删除商品？',
			success: function (res) {
					if (res.confirm) {
						let ids = []
						list.value.forEach(it=>{
								if(it.check){
									ids.push(it.id)
								}
						})
						_del_cart_goods(ids.join(','))
					} else if (res.cancel) {
					}
				}
		})
}
const  _del_cart_goods = (cart_ids:string) =>{
	delCart({id:cart_ids}).then(res=>{
		uni.showToast({
			title:res.msg,
			icon:'none'
		})
		if(res.code==1){
			select_price.value = 0
			is_all.value = false
			_cart_list()
		}
	})
}
const opendeliverytype =() =>{
	let ids = []
	list.value.forEach(it=>{
			if(it.check){
				ids.push(it.id)
			}
	})
	uni.navigateTo({
		url:'/indexpages/goodsBuy/goodsBuy?ids='+ids.join(',')+'&isSwitch=1',
	})
}
const gogoodsdetails =(id:number) =>{
	uni.navigateTo({
		url:'/indexpages/goodsDetail/goodsDetail?id='+id
	})
}
const goshopgoods = (id) =>{
	uni.navigateTo({
		url:'/indexpages/shopgoods/shopgoods?id='+id
	})
}
</script>

<style lang="scss">
@import 'car.scss';
</style>
